<project basedir="./" default="test">
	<property file="build.properties" />

	<target name="param">
		<property name="work.dir" location="work" />
		<property name="work.bin.dir" location="${work.dir}/bin" />
		<property name="build.dir" location="build" />
		<property name="jar.path" location="${build.dir}/${project.name}.jar" />
		<property name="test.jar.path" location="${build.dir}/${project.name}_test.jar" />

		<property name="target.bin.dir" location="${work.bin.dir}" />
		<property name="test.bin.dir" location="${build.dir}/testbin" />
		<property name="build.lib.dir" location="${build.dir}/lib" />
	</target>

	<target name="cleanup" depends="param">
		<echo>delete ${work.bin.dir}</echo>
		<delete dir="${work.bin.dir}" />

		<echo>mkdir ${build.dir}</echo>
		<mkdir dir="${build.dir}" />
	</target>

	<target name="compile" depends="cleanup">
		<echo>delete ${target.bin.dir}</echo>
		<delete dir="${target.bin.dir}" />

		<echo>mkdir ${target.bin.dir}</echo>
		<mkdir dir="${target.bin.dir}" />

		<echo>copy [${target.source.dir}] to [${target.bin.dir}]</echo>
		<copy todir="${target.bin.dir}">
			<fileset dir="${target.source.dir}">
				<include name="**/*.java" />
				<include name="**/*.gif" />
				<include name="**/*.properties" />
				<include name="**/*.txt" />
				<include name="**/*.xls" />
				<include name="**/*.xml" />
			</fileset>
		</copy>

		<echo>copy [${target.lib.dir}] to [${build.lib.dir}]</echo>
		<copy todir="${build.lib.dir}">
			<fileset dir="${target.lib.dir}">
				<include name="*.jar" />
				<include name="*.zip" />
			</fileset>
		</copy>

		<path id="target.classpath">
			<filelist files="${build.dir}" />
			<fileset dir="${build.lib.dir}">
				<include name="*.jar" />
				<include name="*.zip" />
			</fileset>
			<filelist files="${target.jars}" />
		</path>
		<pathconvert targetos="${targetos}" property="string.target.classpath" refid="target.classpath" pathsep=" " />
		<echo>classpath [${string.target.classpath}]</echo>

		<echo>compile [${target.bin.dir}] destdir [${target.bin.dir}]</echo>
		<javac srcdir="${target.bin.dir}" destdir="${target.bin.dir}" includeantruntime="false" debug="true" fork="yes" executable="${compile.exe}" source="${compile.version}" encoding="${compile.encode}">
			<classpath refid="target.classpath" />
			<include name="**/*.java" />
		</javac>
	</target>

	<target name="jar" depends="compile">
		<pathconvert targetos="${targetos}" property="string.jar.classpath" refid="target.classpath" pathsep=" ">
			<map from="${build.dir}/" to="" />
			<map from="${build.dir}" to="." />
		</pathconvert>
		<echo>jar classpath [${string.jar.classpath}]</echo>

		<echo>delete ${jar.path}</echo>
		<delete file="${jar.path}" />

		<echo>jar basedir="${target.bin.dir}"</echo>
		<jar jarfile="${jar.path}" compress="true" index="false" basedir="${target.bin.dir}">
			<include name="*/**/*.class" />
			<include name="*/**/*.java" />
			<include name="*/**/*.properties" />
			<include name="*/**/*.xml" />
			<include name="**/*.txt" />
			<include name="**/*.gif" />
			<include name="**/*.xls" />
			<manifest>
				<attribute name="Built-By" value="${user.name}" />
				<attribute name="Main-Class" value="${mainclass}" />
				<attribute name="Class-Path" value="${string.jar.classpath}" />
			</manifest>
		</jar>
	</target>

	<target name="test_compile" depends="jar">
		<echo>delete ${test.bin.dir}</echo>
		<delete dir="${test.bin.dir}" />

		<echo>mkdir ${test.bin.dir}</echo>
		<mkdir dir="${test.bin.dir}" />

		<echo>copy [${test.lib.dir}] to [${build.lib.dir}]</echo>
		<copy todir="${build.lib.dir}">
			<fileset dir="${test.lib.dir}">
				<include name="*.jar" />
				<include name="*.zip" />
			</fileset>
		</copy>

		<echo>copy [${test.source.dir}] to [${test.bin.dir}]</echo>
		<copy todir="${test.bin.dir}">
			<fileset dir="${test.source.dir}">
				<include name="**/*.java" />
				<include name="**/*.gif" />
				<include name="**/*.properties" />
				<include name="**/*.txt" />
				<include name="**/*.xls" />
				<include name="**/*.xml" />
			</fileset>
		</copy>

		<path id="test.classpath">
			<fileset file="${jar.path}" />
			<fileset dir="${test.lib.dir}">
				<include name="*.jar" />
				<include name="*.zip" />
			</fileset>
			<filelist files="${test.jars}" />
		</path>
		<pathconvert targetos="${targetos}" property="string.test.classpath" refid="test.classpath" pathsep=" " />
		<echo>classpath [${string.test.classpath}]</echo>

		<javac srcdir="${test.bin.dir}" destdir="${test.bin.dir}" includeantruntime="false" debug="true" fork="yes" executable="${compile.exe}" source="${compile.version}" encoding="${compile.encode}">
			<classpath path="${jar.path}" />
			<classpath refid="target.classpath" />
			<classpath refid="test.classpath" />
			<include name="**/*.java" />
		</javac>
	</target>

	<target name="test_jar" depends="test_compile">
		<pathconvert targetos="${targetos}" property="string.test_jar.classpath" refid="test.classpath" pathsep=" ">
			<map from="${build.dir}/" to="" />
			<map from="${build.dir}" to="." />
		</pathconvert>
		<echo>classpath [${string.test_jar.classpath}]</echo>

		<echo>delete ${test.jar.path}</echo>
		<delete file="${test.jar.path}" />

		<echo>jar jarfile="${test.jar.path}" basedir="${test.bin.dir}"</echo>
		<jar jarfile="${test.jar.path}" compress="true" index="false" basedir="${test.bin.dir}">
			<include name="*/**/*.class" />
			<include name="*/**/*.java" />
			<include name="*/**/*.properties" />
			<include name="*/**/*.xml" />
			<include name="**/*.txt" />
			<include name="**/*.gif" />
			<include name="**/*.xls" />
		</jar>
	</target>

	<target name="test" depends="test_compile">
		<echo>mkdir ${reports.dir}</echo>
		<mkdir dir="${reports.dir}" />

		<path id="junit.classpath">
			<fileset file="${jar.path}" />
			<fileset dir="${build.lib.dir}">
				<include name="*.jar" />
				<include name="*.zip" />
			</fileset>
			<filelist files="${test.jars}" />
		</path>
		<pathconvert targetos="${targetos}" property="string.junit.classpath" refid="junit.classpath" pathsep=" " />
		<echo>classpath [${string.junit.classpath}]</echo>

		<junit printsummary="yes" haltonfailure="yes">
			<classpath path="${test.bin.dir}" />
			<classpath refid="junit.classpath" />
			<formatter type="xml" />
			<batchtest fork="yes" todir="${reports.dir}">
				<fileset dir="${test.bin.dir}">
					<include name="**/*Test*.class" />
				</fileset>
			</batchtest>
		</junit>
	</target>
</project>